package com.bluesmart.mia;

import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.DensityUtil;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;
import com.widget.CircleImageView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class NoteViewActivity extends BaseActivity implements
		OnClickListener {
	MyAdapter mAdapter;
	int mType = 0;
	int mPage = 1;
	ArrayList<JSONObject> mArrayList = new ArrayList<JSONObject>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_view);
		((TextView)findViewById(R.id.center)).setText(getString(R.string.note));

		findViewById(R.id.icon).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.btn0).setOnClickListener(this);
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
		findViewById(R.id.btn3).setOnClickListener(this);
		findViewById(R.id.add).setOnClickListener(this);
		
		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
		ptrlvHeadlineNews.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.BOTH);
		ptrlvHeadlineNews.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() 
		{
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) 
			{
				sendChartHttpRequest(false);
			}
		});
		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		listview.setVerticalScrollBarEnabled(false);
		listview.setOnItemLongClickListener(new OnItemLongClickListener(){
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				showQuerySearch(arg2 - 1);
				return true;
			}
			
		});
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
		
		initRightMenu();
		sendChartHttpRequest(true);
		
		((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.rgb(13,128,214));
	}
	
	void showQuerySearch(final int index) {
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_query);
		mDialog.findViewById(R.id.left).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
					}
				});
		mDialog.findViewById(R.id.right).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						deleteItem(index);
						mDialog.dismiss();
					}
				});
		((TextView)mDialog.findViewById(R.id.content)).setText(getString(R.string.delete_tip));
		mDialog.show();
	}
	
	void deleteItem(final int index)
	{
		String noteid = mArrayList.get(index).getString("dataid");
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					ToastUtil.showToastWithAlertPic(R.string.success);
					mArrayList.remove(index);
					mAdapter.notifyDataSetChanged();
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else
				{
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("handtype", "delete");
		params.put("noteid", noteid);
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/NoteHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
	/*
	void initMenu()
	{
		final OnClickListener listener = new OnClickListener(){
			@Override
			public void onClick(View v) {
				if (v.getId() == 100) {
					Intent intent = new Intent(NoteViewActivity.this,NoteActivity.class);
					intent.putExtra("type", 1);
					startActivity(intent);
				} else if (v.getId() == 101) {
					Intent intent = new Intent(NoteViewActivity.this,NoteFoodActivity.class);
					intent.putExtra("type", 5);
					startActivity(intent);
				} else if (v.getId() == 102) {
					Intent intent = new Intent(NoteViewActivity.this,NoteHeightActivity.class);
					intent.putExtra("type", 3);
					startActivity(intent);
				} else if (v.getId() == 103) {
					Intent intent = new Intent(NoteViewActivity.this,NoteSleepActivity.class);
					intent.putExtra("type", 4);
					startActivity(intent);
				} else if (v.getId() == 104) {
					Intent intent = new Intent(NoteViewActivity.this,NoteGoalActivity.class);
					intent.putExtra("type", 6);
					startActivity(intent);
				} 
			}
		};
		
		composerLayout clayout = (composerLayout) findViewById(R.id.menu);
		clayout.init(new int[] { R.drawable.a72,
				R.drawable.a73, R.drawable.a74,
				R.drawable.a75, R.drawable.a76,}, R.drawable.composer_button,
				R.drawable.composer_icn_plus, composerLayout.RIGHTBOTTOM, DensityUtil.dip2px(this, 100),300);
		clayout.setButtonsOnClickListener(listener);
	}*/
	
	protected void onResume()
	{
		super.onResume();
		setUserImage();
		
		if(((DemoApp)getApplicationContext()).isNewMsg)
		{
			((TextView)slidingMenu.findViewById(R.id.btn5)).setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.bg_news, 0, R.drawable.a17, 0);
			((ImageButton)findViewById(R.id.right)).setImageResource(R.drawable.unread);
		}
		else
		{
			((TextView)slidingMenu.findViewById(R.id.btn5)).setCompoundDrawablesWithIntrinsicBounds(
					0, 0, R.drawable.a17, 0);
			((ImageButton)findViewById(R.id.right)).setImageResource(R.drawable.a25);
		}
	}
	
	void setUserImage()
	{
		if(((DemoApp)getApplicationContext()).mBabyList != null)
		{
			JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();
			
			TextView tv = (TextView)slidingMenu.findViewById(R.id.btn10);
			tv.setTag(obj.toString());
			
			if(obj != null)
			{
				ImageLoaderHelper.displayImage(R.drawable.a53, (CircleImageView)findViewById(R.id.icon), 
						obj.getString("image"));
			}
		}
	}
	
	SlidingMenu slidingMenu = null;
	void initRightMenu()
	{
		OnClickListener listener = new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				menuBtn(arg0);
			}
		};
		
		slidingMenu = new SlidingMenu(this);
		slidingMenu.setMode(SlidingMenu.RIGHT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		slidingMenu.setMenu(R.layout.right_menu);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setSecondaryShadowDrawable(R.drawable.shadow);
		slidingMenu.setFadeDegree(0.35f);
		
		slidingMenu.findViewById(R.id.btn4).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn5).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn6).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn7).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn8).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn9).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn10).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn11).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn12).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn13).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn14).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn15).setOnClickListener(listener);
		slidingMenu.findViewById(R.id.btn16).setOnClickListener(listener);
		
		sendHttpRequest();
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				
				JSONObject json = JSON.parseObject(response);
				if(json.keySet().size() > 0)
				{
					slidingMenu.findViewById(R.id.btn10).setVisibility(View.VISIBLE);
					((TextView)slidingMenu.findViewById(R.id.btn10)).setText(json.getString("deviceName"));
					((TextView)slidingMenu.findViewById(R.id.btn9)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
					slidingMenu.findViewById(R.id.btn9).setOnClickListener(null);
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();
		if(obj == null) return;
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", (((DemoApp)getApplicationContext()).getSeletedBaby().getString("babyid")));

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/DownLoadDeviceInfo.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	void menuBtn(View arg0)
	{
		slidingMenu.toggle(false);
		
		switch(arg0.getId())
		{
		case R.id.btn5:
			startActivity(new Intent(this,NotificationActivity.class));
			break;

		case R.id.btn6:
			startActivity(new Intent(this,GeneralActivity.class));
			break;
			
		case R.id.btn4:
			startActivity(new Intent(this,AccountActivity.class));
			break;
			
		case R.id.btn9:
			startActivity(new Intent(this,AddDeviceActivity.class));
			break;

		case R.id.btn10:
		case R.id.btn11:
		case R.id.btn12:
		case R.id.btn13:
		case R.id.btn14:
		case R.id.btn15:
		case R.id.btn16:
		{
			Intent intent = new Intent(this,DeviceInfoActivity.class);
			intent.putExtra("obj", (String)arg0.getTag());
			startActivity(intent);
			break;
		}
		}
	}
	
	@Override
	public void onClick(View arg0) {

		switch(arg0.getId())
		{
		case R.id.icon:
			startActivity(new Intent(this,BabyViewActivity.class));
			break;
			
		case R.id.right:
			slidingMenu.toggle(true);
			break;
			
		case R.id.add:
			startActivity(new Intent(this,NewNoteActivity.class));
			break;
			
		case R.id.btn0:
			mType = 0;
			sendChartHttpRequest(true);
			
			((Button)findViewById(R.id.btn0)).setTextColor(Color.parseColor("#0fc0db"));
			((Button)findViewById(R.id.btn1)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn2)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn3)).setTextColor(Color.parseColor("#AAAAAA"));
			
			((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.rgb(13,128,214));
			((Button)findViewById(R.id.btn1)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn2)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn3)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			break;
			
		case R.id.btn1:
			mType = 1;
			sendChartHttpRequest(true);
			
			((Button)findViewById(R.id.btn0)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn1)).setTextColor(Color.parseColor("#0fc0db"));
			((Button)findViewById(R.id.btn2)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn3)).setTextColor(Color.parseColor("#AAAAAA"));
			
			((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn1)).setBackgroundColor(Color.rgb(13,128,214));
			((Button)findViewById(R.id.btn2)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn3)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			break;

		case R.id.btn2:
			mType = 5;
			sendChartHttpRequest(true);
			
			((Button)findViewById(R.id.btn0)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn1)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn2)).setTextColor(Color.parseColor("#0fc0db"));
			((Button)findViewById(R.id.btn3)).setTextColor(Color.parseColor("#AAAAAA"));
			
			((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn1)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn2)).setBackgroundColor(Color.rgb(13,128,214));
			((Button)findViewById(R.id.btn3)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			break;

		case R.id.btn3:
			mType = 6;
			sendChartHttpRequest(true);
			
			((Button)findViewById(R.id.btn0)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn1)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn2)).setTextColor(Color.parseColor("#AAAAAA"));
			((Button)findViewById(R.id.btn3)).setTextColor(Color.parseColor("#0fc0db"));
			
			((Button)findViewById(R.id.btn0)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn1)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn2)).setBackgroundColor(Color.parseColor("#e5e8dd"));
			((Button)findViewById(R.id.btn3)).setBackgroundColor(Color.rgb(13,128,214));
			break;
		}
	}
	
	public void sendChartHttpRequest(boolean show) {
		if(show)
		{
			mArrayList.removeAll(mArrayList);
			mPage = 1;
			mAdapter.notifyDataSetChanged();
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, show, false) {
			@Override
			public void onSuccess(String response) {
				mPage++;
				JSONArray mArray = JSON.parseArray(response);
				if(mArray != null)
				{
					int count = mArray.size();
					for(int i = 0; i < count; i++)
					{
						mArrayList.add(mArray.getJSONObject(i));
					}
				}
				
				mAdapter.notifyDataSetChanged();

				final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
				ptrlvHeadlineNews.onRefreshComplete();
			}
		};
		
		JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();

		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", obj.getString("babyid"));
		params.put("handType", "get");
		params.put("pageindex", "" + mPage);
		params.put("notetype", "" + mType);
	
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/NoteHandler.ashx", JSON.toJSONString(params), loginHandler);
	}

	class MyAdapter extends BaseAdapter implements OnClickListener{
		public int getCount() {
			return mArrayList.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_note, null);
				foodHold.img0 = (ImageView) convertView.findViewById(R.id.img0);
				foodHold.img1 = (ImageView) convertView.findViewById(R.id.img1);
				foodHold.img2 = (ImageView) convertView.findViewById(R.id.img2);
				foodHold.img3 = (ImageView) convertView.findViewById(R.id.img3);
				foodHold.img4 = (ImageView) convertView.findViewById(R.id.img4);
				foodHold.img5 = (ImageView) convertView.findViewById(R.id.img5);
				foodHold.img6 = (ImageView) convertView.findViewById(R.id.img6);
				foodHold.img7 = (ImageView) convertView.findViewById(R.id.img7);
				foodHold.img8 = (ImageView) convertView.findViewById(R.id.img8);
				foodHold.icon = (ImageView) convertView.findViewById(R.id.icon);
				foodHold.title = (TextView) convertView.findViewById(R.id.title);
				foodHold.name = (TextView) convertView.findViewById(R.id.name);
				foodHold.content = (TextView) convertView.findViewById(R.id.content);
				foodHold.date = (TextView) convertView.findViewById(R.id.date);
				foodHold.time = (TextView) convertView.findViewById(R.id.time);
				foodHold.note_text = (TextView) convertView.findViewById(R.id.note_text);
				foodHold.layout0 = (LinearLayout) convertView.findViewById(R.id.layout0);
				foodHold.layout1 = (LinearLayout) convertView.findViewById(R.id.layout1);
				foodHold.layout2 = (LinearLayout) convertView.findViewById(R.id.layout2);
				
				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}
			
			int count = 0;
			//{"adddate":"2016-01-16","addtime":"15:23","adduser":"宝宝妈","content":"哈哈哈哈哈哈哈哈哈哈笑","dataid":"yVMOuq5QDVs=",
//			"food":"米汤","height_weight":"30.0-3.0","imgs":",http://mia.ijiebao.com:8080/File/1363/90F9446FC6DB67BE182F4B718376F655.png,http://mia.ijiebao.com:80
//			80/File/1363/D07C6D411DC76630717EBED3C695C34A.png,http://mia.ijiebao.com:8080/File/1363/5434C1D6F6A06E00DC49D1F91B053717.png,http://mia.ijiebao.com:8080/File/
//			1363/E7F8A3B0D3E778DCD0A1BFA0B6878D19.png,http://mia.ijiebao.com:8080/File/1363/E642891ABB2C9ABACF3D4F20845103BE.png","milestone":"哭","notetype":1,"position
//			":"","quicknote":"宝宝进入“猛长期”","sleep":"2016-01-15 16:00 - 2016-01-16 00:00"}
			JSONObject item = mArrayList.get(position);
			String img = item.getString("imgs");
			if(img.length() > 0 && img.charAt(0) == ',') img = img.substring(1);
			String imgList[] = img.split(",");
			if(img.length() > 0) count = imgList.length;
			
			String noteText = "";
			String tmp = item.getString("quicknote");
			if(tmp != null && tmp.length() > 0)
			{
				noteText += getString(R.string.note2) + ":" + tmp;
			}
				
			tmp = item.getString("food");
			if(tmp != null && tmp.length() > 0)
			{
				if(noteText.length() > 0) noteText += "\n";
				noteText += getString(R.string.note5) + ":" + tmp;
			}
			tmp = item.getString("height_weight");
			if(tmp != null && tmp.length() > 0)
			{
				if(noteText.length() > 0) noteText += "\n";
				noteText += getString(R.string.note3) + ":" + tmp;
			}
			tmp = item.getString("sleep");
			if(tmp != null && tmp.length() > 0)
			{
				if(noteText.length() > 0) noteText += "\n";
				noteText += getString(R.string.note4) + ":" + tmp;
			}
			tmp = item.getString("milestone");
			if(tmp != null && tmp.length() > 0)
			{
				if(noteText.length() > 0) noteText += "\n";
				noteText += getString(R.string.note6) + ":" + tmp;
			}
			
			foodHold.note_text.setVisibility(View.GONE);
			if(noteText.length() > 0) 
			{
				foodHold.note_text.setVisibility(View.VISIBLE);
				foodHold.note_text.setText(noteText);
			}
			
			foodHold.img0.setTag(String.valueOf(position));
			foodHold.img1.setTag(String.valueOf(position));
			foodHold.img2.setTag(String.valueOf(position));
			foodHold.img3.setTag(String.valueOf(position));
			foodHold.img4.setTag(String.valueOf(position));
			foodHold.img5.setTag(String.valueOf(position));
			foodHold.img6.setTag(String.valueOf(position));
			foodHold.img7.setTag(String.valueOf(position));
			foodHold.img8.setTag(String.valueOf(position));

			foodHold.img0.setOnClickListener(this);
			foodHold.img1.setOnClickListener(this);
			foodHold.img2.setOnClickListener(this);
			foodHold.img3.setOnClickListener(this);
			foodHold.img4.setOnClickListener(this);
			foodHold.img5.setOnClickListener(this);
			foodHold.img6.setOnClickListener(this);
			foodHold.img7.setOnClickListener(this);
			foodHold.img8.setOnClickListener(this);
			
			if(count > 0) foodHold.layout0.setVisibility(View.VISIBLE);
			else foodHold.layout0.setVisibility(View.GONE);
			
			if(count > 3) foodHold.layout1.setVisibility(View.VISIBLE);
			else foodHold.layout1.setVisibility(View.GONE);
			
			if(count > 6) foodHold.layout2.setVisibility(View.VISIBLE);
			else foodHold.layout2.setVisibility(View.GONE);
			
			for(int i = 0; i < 9; i++)
			{
				ImageView imageview = (ImageView)convertView.findViewById(R.id.img0 + i);
				if(i < count)
				{
					imageview.setVisibility(View.VISIBLE);
					ImageLoaderHelper.displayImage(R.drawable.a37, imageview,imgList[i]);
				}
				else
				{
					imageview.setVisibility(View.INVISIBLE);
				}
			}
			
			String content = item.getString("content");
			foodHold.content.setVisibility(View.GONE);
			if(content != null && content.length() > 0)
			{
				foodHold.content.setText(content);
				foodHold.content.setVisibility(View.VISIBLE);
			}
			
			foodHold.name.setText(item.getString("adduser"));
			foodHold.date.setText(item.getString("adddate").substring(5));
			foodHold.time.setText(item.getString("addtime"));
			
			if (item.getInteger("notetype") == 1 || item.getInteger("notetype") == 8) {
				foodHold.icon.setImageResource(R.drawable.notetype2);
				foodHold.title.setText(getString(R.string.note1));
				if(item.getInteger("notetype") == 8) foodHold.title.setText(getString(R.string.note0));
			}else if (item.getInteger("notetype") == 2) {
				foodHold.icon.setImageResource(R.drawable.notetype4);
				foodHold.title.setText(getString(R.string.note2));
			}else if (item.getInteger("notetype") == 5) {
				foodHold.icon.setImageResource(R.drawable.notetype5);
				foodHold.title.setText(getString(R.string.note5));
			} else if (item.getInteger("notetype") == 3) {
				foodHold.icon.setImageResource(R.drawable.notetype6);
				foodHold.title.setText(getString(R.string.note3));
			} else if (item.getInteger("notetype") == 4) {
				foodHold.icon.setImageResource(R.drawable.notetype7);
				foodHold.title.setText(getString(R.string.note4));
			} else if (item.getInteger("notetype") == 6) {
				foodHold.icon.setImageResource(R.drawable.notetype8);
				foodHold.title.setText(getString(R.string.note6));
			} 

			foodHold.name.setVisibility(View.GONE);
			foodHold.title.setText(item.getString("adduser") + " " + getString(R.string.add_a_note));
			
			return convertView;
		}

		@Override
		public void onClick(View arg0) {
			int position = Integer.valueOf((String)arg0.getTag());
			
			JSONObject item = mArrayList.get(position);
			String img = item.getString("imgs");
			int index = arg0.getId() - R.id.img0;
			
			Intent intent = new Intent(NoteViewActivity.this,ImageShowActivity.class);
			intent.putExtra("imgs", img);
			intent.putExtra("index", index);
			startActivity(intent);
		}
	}

	public final class FoodHolder {
		public ImageView icon;
		public ImageView img0;
		public ImageView img1;
		public ImageView img2;
		public ImageView img3;
		public ImageView img4;
		public ImageView img5;
		public ImageView img6;
		public ImageView img7;
		public ImageView img8;
		public TextView title;
		public TextView date;
		public TextView time;
		public TextView name;
		public TextView content;
		public TextView note_text;
		
		public LinearLayout layout0;
		public LinearLayout layout1;
		public LinearLayout layout2;
	}
}
